/**
* @ProjectName: ShopSytem
* @Title: AddWord.java 
* @Package com.element5th.shop.servlet.leaveword 
* @Description: 添加留言
* @author 第五元素小组   
* @date 2012-10-26 16:50:55 
* @version V1.0   
*/
package com.element5th.shop.servlet.leaveword;

import java.io.IOException;
import java.sql.Date;


import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.element5th.shop.comm.DaoFactory;
import com.element5th.shop.dao.leaveword.IWordDao;
import com.element5th.shop.dto.BaseDto;
import com.element5th.shop.entity.LeaveWord;
import com.element5th.shop.entity.User;

/**
 * Servlet implementation class AddWord
 */
@WebServlet("/word/add")
public class AddWord extends HttpServlet {
	private static final long serialVersionUID = 1L;
       

	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		//封装留言的数据；
		BaseDto dto = new BaseDto();
		LeaveWord leaveWord = new LeaveWord();
		
		User user = (User)request.getSession().getAttribute("User");
	    String wordLeavecontent = request.getParameter("message");
	    Date wordLeavetime = new Date(System.currentTimeMillis());
	    
	    leaveWord.setUserId(user.getUserId());
	    leaveWord.setWordLeavecontent(wordLeavecontent);
	    leaveWord.setWordLeavetime(wordLeavetime);
	    
	    dto.setLeaveWord(leaveWord);
	    
	    //将留言信息存储到数据库中，并返回是否成功；
	    IWordDao iWordDao= DaoFactory.getWordDao();
	    boolean flag = iWordDao.addWord(dto);
	    if(flag){
	    	request.getSession().setAttribute("LeaveFlag", "谢谢您的留言！");
	    }else{
	    	request.getSession().setAttribute("LeaveFlag", "您的留言失败，请重新留言！");
	    }
	    request.getRequestDispatcher("/page/bbs/LeaveWord.jsp").forward(request, response);
	}

}
